package com.atguigu.dao.impl;

import com.atguigu.dao.NginxServiceConfigProxyDao;
import com.atguigu.pojo.NginxServiceConfigProxy;
import com.atguigu.pojo.Server;

import java.util.List;

public class NginxServiceConfigProxyDaoImpl  extends BaseDao implements NginxServiceConfigProxyDao {
    @Override
    public int addNginxServiceConfigProxy(NginxServiceConfigProxy nginxServiceConfigProxy) {
        System.out.println("NginxServiceConfigProxyDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_nginxServiceConfigProxy(`id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use`) values(?,?,?,?,?,?,?,?,?,?);";
            return update(sql,nginxServiceConfigProxy.getId(),nginxServiceConfigProxy.getServerId(),
                    nginxServiceConfigProxy.getServiceId(),nginxServiceConfigProxy.getFrontDomain(),
                    nginxServiceConfigProxy.getFrontPort(),nginxServiceConfigProxy.getProjectModuleTypeId(),nginxServiceConfigProxy.getProjectId(),
                    nginxServiceConfigProxy.getProjectModuleId(),nginxServiceConfigProxy.getTargetNodeId(),nginxServiceConfigProxy.getUse());
    }

    @Override
    public int deleteNginxServiceConfigProxyById(String id) {
        String sql="delete from t_nginxServiceConfigProxy where id=?";
        return update(sql,id);
    }

    @Override
    public int updateNginxServiceConfigProxy(NginxServiceConfigProxy nginxServiceConfigProxy) {
        String sql="update t_nginxServiceConfigProxy set `serverId`=?,`serviceId`=?,`frontDomain`=?,`frontPort`=?,`projectModuleTypeId`=?,`projectId`=?,`projectModuleId`=?,`targetNodeId`=?,`use`=? where id=?";
        return update(sql,nginxServiceConfigProxy.getServerId(),
                nginxServiceConfigProxy.getServiceId(),nginxServiceConfigProxy.getFrontDomain(),
                nginxServiceConfigProxy.getFrontPort(),nginxServiceConfigProxy.getProjectModuleTypeId(),nginxServiceConfigProxy.getProjectId(),
                nginxServiceConfigProxy.getProjectModuleId(),nginxServiceConfigProxy.getTargetNodeId(),nginxServiceConfigProxy.getUse(),nginxServiceConfigProxy.getId());
    }

    @Override
    public NginxServiceConfigProxy queryNginxServiceConfigProxyById(String id) {
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where id=?";
        return queryForOne(NginxServiceConfigProxy.class,sql,id);
    }
    @Override
    public NginxServiceConfigProxy queryNginxServiceConfigProxyByTargetNodeId(String targetNodeId){
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where targetNodeId=?";
        return queryForOne(NginxServiceConfigProxy.class,sql,targetNodeId);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxysByTargetNodeId(String targetNodeId){
    	String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where targetNodeId=?";
        return queryForList(NginxServiceConfigProxy.class,sql,targetNodeId);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxys() {
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy";
        return queryForList(NginxServiceConfigProxy.class,sql);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByServiceId(String serviceId){
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where serviceId =?";
        return queryForList(NginxServiceConfigProxy.class,sql,serviceId);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByFrontPort(String frontPort) {
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where frontPort =?";
        return queryForList(NginxServiceConfigProxy.class,sql,frontPort);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByServiceIdFrontDomainPort(String serviceId,String frontDomain,String frontPort) {
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy where serviceId =? and frontDomain =? and frontPort =?";
        return queryForList(NginxServiceConfigProxy.class,sql,serviceId,frontDomain,frontPort);
    }
    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_nginxServiceConfigProxy";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<NginxServiceConfigProxy> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `id`,`serverId`,`serviceId`,`frontDomain`,`frontPort`,`projectModuleTypeId`,`projectId`,`projectModuleId`,`targetNodeId`,`use` from t_nginxServiceConfigProxy limit ?,?";
        return queryForList(NginxServiceConfigProxy.class,sql,begin,pageSize);

    }
}
